package com.example.webjsdemo

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.service.carrier.CarrierMessagingService
import android.webkit.*
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import com.huawei.hms.push.HmsMessaging
import com.jeremyliao.liveeventbus.LiveEventBus

class MainActivity : AppCompatActivity() {

    private var mWebView: WebView? = null

    @SuppressLint("SetJavaScriptEnabled")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        mWebView = findViewById(R.id.webView)
        mWebView?.settings?.apply {
            javaScriptEnabled = true
            allowFileAccess = true
            domStorageEnabled = true
            loadsImagesAutomatically = true
            setSupportZoom(true)
            builtInZoomControls = true
            useWideViewPort = true
            displayZoomControls = false
            layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
        }
        mWebView?.addJavascriptInterface(AndroidJs(this@MainActivity), "injectedObject")
        mWebView?.webViewClient = MyWebViewClient()
//        mWebView?.loadUrl("http://192.168.2.209/local2/#/")
        mWebView?.loadUrl("http://www.sxjbjt.com:9530/steel-scrap/#/")

        startService(Intent(this@MainActivity, ScreenService::class.java))

        LiveEventBus.get<String>("123").observe(this) {
            connectJsWebSocket()
        }
        LiveEventBus.get<String>("456").observe(this) {
            logout()
        }

        setAutoInitEnabled(true)
    }

    private fun setAutoInitEnabled(isEnable: Boolean) {
        HmsMessaging.getInstance(this).isAutoInitEnabled = isEnable
    }

    private fun connectJsWebSocket(){
        mWebView?.evaluateJavascript("javascript:getwebsocket()",
            @RequiresApi(Build.VERSION_CODES.LOLLIPOP_MR1)
            object : CarrierMessagingService.ResultCallback<String>, ValueCallback<String> {
                override fun onReceiveResult(result: String) {
                }

                override fun onReceiveValue(value: String?) {
                }
            })
    }

    private fun logout(){
        mWebView?.evaluateJavascript("javascript:logout()",
            @RequiresApi(Build.VERSION_CODES.LOLLIPOP_MR1)
            object : CarrierMessagingService.ResultCallback<String>, ValueCallback<String> {
                override fun onReceiveResult(result: String) {
                }

                override fun onReceiveValue(value: String?) {
                }
            })
    }

    class MyWebViewClient : WebViewClient() {
        @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
        override fun shouldOverrideUrlLoading(
            view: WebView?,
            request: WebResourceRequest?
        ): Boolean {
            view?.loadUrl(request?.url.toString())
            return true
        }

        @RequiresApi(Build.VERSION_CODES.M)
        override fun onReceivedError(
            view: WebView?,
            request: WebResourceRequest?,
            error: WebResourceError?
        ) {
            super.onReceivedError(view, request, error)
        }
    }

    override fun onBackPressed() {
        super.onBackPressed()
        logout()
    }
}